package com.bhcj.telling.utils;

import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileUtils {

    private static final String JSON_SUFFIX = ".json";

    public static String readJsonFile(AssetManager assetManager, String fileName){
        StringBuilder jsonBuf = new StringBuilder(0);
        String jsonFileName = getJsonFileName(fileName);
        try (BufferedReader br = new BufferedReader(new InputStreamReader(assetManager.open(jsonFileName)))){
            String line = null;
            while ((line = br.readLine()) != null){
                jsonBuf.append(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return jsonBuf.toString();
    }

    private static String getJsonFileName(String fileName){
        return fileName + JSON_SUFFIX;
    }
}
